# -*- autoconf -*-
#
# Copyright (c) 2011-2015 The University of Tennessee and The University
#                         of Tennessee Research Foundation.  All rights
#                         reserved.
# Copyright (c) 2011-2015 INRIA.  All rights reserved.
# Copyright (c) 2011-2015 Universite Bordeaux 1
# Copyright (c) 2015      Cisco Systems, Inc.  All rights reserved.
# Copyright (c) 2015      Intel, Inc. All rights reserved.
# Copyright (c) 2019      Research Organization for Information Science
#                         and Technology (RIST).  All rights reserved.
# Copyright (c) 2020      Amazon.com, Inc. or its affiliates.
#                         All Rights reserved.
# Copyright (c) 2021      Google, LLC. All rights reserved.
# $COPYRIGHT$
#
# Additional copyrights may follow
#
# $HEADER$
#

AC_DEFUN([OPAL_CONFIG_TREEMATCH], [
    OPAL_VAR_SCOPE_PUSH(treematch_files_local)

    OPAL_3RDPARTY_WITH([treematch], [treematch], [package_treematch])

    if test -z "$opal_config_treematch_happy" ; then
        AC_ARG_WITH([treematch],
                    [AS_HELP_STRING([--with-treematch(=DIR)],
                                    [Build TreeMatch topology support, optionally adding DIR/include, DIR/lib, and DIR/lib64 to the search path for headers and libraries])],
		    [], [with_treematch=yes])
        AC_ARG_WITH([treematch-include],
                    [AS_HELP_STRING([--with-treematch-include(=DIR)],
                                    ["Search for TreeMatch headers in DIR"])])
        AC_ARG_WITH([treematch-libdir],
                    [AS_HELP_STRING([--with-treematch-libdir(=DIR)],
                                    ["Search for TreeMatch libraries in DIR"])])

        treematch_files_local=no
        opal_config_treematch_happy=0

	if test "$with_treematch" != no ; then
	    # Argument sanity checks.
	    if (test -z "$with_treematch_include" && test -n "$with_treematch_libdir") ||
		   (test -n "$with_treematch_include" && test -z "$with_treematch_libdir") ; then
		AC_MSG_ERROR([specified only one of --with-treematch-include or --with-treematch-libdir. Both (or neither) should be specified])
	    fi
	    if test -n "$with_treematch_include" && test "$with_treematch" != yes ; then
		AC_MSG_ERROR([specified both --with-treematch-include and either --with-treematch=path or --without-treematch])
	    fi

	    AC_MSG_CHECKING([TreeMatch source])
	    if test -n "$with_treematch_include" || test "$with_treematch" != yes ; then
		if test -z "$with_treematch_include" ; then
		    with_treematch_include="$with_treematch/include"
		    with_treematch_libdir="$with_treematch/lib"
		fi
		AC_MSG_RESULT([user provided])
	    else
		treematch_files_local=yes
		with_treematch_include="$OMPI_TOP_SRCDIR/3rd-party/treematch"
		with_treematch_libdir="$OMPI_TOP_BUILDDIR/3rd-party/treematch"
		AC_MSG_RESULT([in tree (3rd-party)])
	    fi

	    OPAL_CHECK_WITHDIR([treematch],
			       [$with_treematch_include],
			       [tm_tree.h])
	    if test $treematch_files_local == no ; then
		OPAL_CHECK_WITHDIR([treematch-libdir], [$with_treematch_libdir], [libtreematch.*])
		OPAL_CHECK_PACKAGE([topo_treematch],
				   [treematch.h],
				   [treematch],
				   [tm_build_synthetic_topology],
				   [],
				   [${with_treematch_include%include}],
				   [$with_treematch_libdir],
				   [opal_config_treematch_happy=1],
				   [AC_MSG_ERROR([TreeMatch support requested but not found.  Aborting])])
	    else
		opal_config_treematch_happy=1
	    fi
	fi

	if test $opal_config_treematch_happy -eq 1 && test $treematch_files_local = yes ; then
	    AS_IF([! test -d $OMPI_TOP_BUILDDIR/3rd-party/treematch],
                  [mkdir -p $OMPI_TOP_BUILDDIR/3rd-party/treematch])
	    AC_CONFIG_FILES([3rd-party/treematch/Makefile])
	    cat > $OMPI_TOP_BUILDDIR/3rd-party/treematch/config.h << EOF
/*
 * This file is automatically generated by configure.  Edits will be lost
 *
 * This is an dummy config.h in order to prevent the embedded treematch from using
 * the config.h from the embedded hwloc
 *
 * see https://github.com/open-mpi/ompi/pull/6185#issuecomment-458807930
 */
EOF
	    opal_config_treematch_LIBS=$OMPI_TOP_BUILDDIR/3rd-party/treematch/libtreematch.la
	    opal_config_treematch_LDFLAGS=
	else
	    opal_config_treematch_LIBS="-ltreematch"
	    opal_config_treematch_LDFLAGS="-L$with_treematch_libdir"
	fi
    fi

    if test $opal_config_treematch_happy -eq 1 ; then
	$1_CPPFLAGS="[$]$1_CPPFLAGS -I$with_treematch_include"
	$1_LDFLAGS="[$]$1_LDFLAGS $opal_config_treematch_LDFLAGS"
	$1_LIBS="[$]$1_LIBS $opal_config_treematch_LIBS"
    fi

    OPAL_3RDPARTY_SUBDIRS="$OPAL_3RDPARTY_SUBDIRS treematch"
    OPAL_3RDPARTY_DIST_SUBDIRS="$OPAL_3RDPARTY_DIST_SUBDIRS treematch"
    
    AS_IF([test $opal_config_treematch_happy -eq 1], [$2], [$3])
    OPAL_VAR_SCOPE_POP
])

    
